/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triploa;
import java.io.IOException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;
/**
 *
 * @author Edoardo
 */
public class Message {
    private int userId;
    private String sender,text,title;
    StringBuffer receivers;
    private boolean priority,isRead;
   private Message()
{
    userId=0;
    sender=null;
    receivers=null;
    text=null;
    title=null;
}
    public static final String MESSAGE_TAG="a:Message";
public static final String ID_TAG="a:Id";
public static final String SENDER_TAG="a:Sender";
public static final String RECEIVER_TAG="a:Receiver";
public static final String TITLE_TAG="a:Title";
public static final String TEXT_TAG="a:Text";
public static final String PRIORITY_TAG="a:Priority";
public static final String IS_READ_TAG="a:IsRead";
/**
 * call for getting a new instance of this class from xml
 * @param p Parser
 * @return a new instance of FeedBack
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public static Message create(KXmlParser p)throws IOException, XmlPullParserException
{
    Message l=new Message();
    l.update(p);
    if( l.getText()==null&&l.getTitle()==null&&l.getSender()==null) return null;
    return l;
}
    /**
 * Update this object class from xml parser
 * @param p Parser
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
public void update(KXmlParser p) throws IOException, XmlPullParserException
{
       boolean exit = false,rec=false, travel = false;
        int token = -1;
        String n="",lasttag=n;
        while (!exit) {
            token = p.next();
            switch (token) {
                case KXmlParser.START_DOCUMENT:
                    break;
                case KXmlParser.END_DOCUMENT:
                    exit = true;  //exit form while
                    break;
                case KXmlParser.START_TAG:
                    System.out.println(p.getName());
                    if (p.getName().equals(MESSAGE_TAG)) {
                        travel = true;
                    }
                    if (p.getName().equals(RECEIVER_TAG)) {
                        rec = true;
                        receivers=new StringBuffer();
                    }
                    if(travel) lasttag=p.getName();
                    break;
                case KXmlParser.END_TAG:
                    if (travel) {
                        if (p.getName().equals(MESSAGE_TAG)) {
                            travel = false;
                            exit = true;
                            break;
                        }
                    }
                    if (p.getName().equals(RECEIVER_TAG)) {
                        rec = false;
                         
                    }
                    lasttag=n;
                    break;
                case KXmlParser.TEXT:
                    if(rec){
                        if(receivers.length()!=0)
                        {
                            receivers.append(',');
                        }
                        receivers.append(p.getText());
                    }
                    if (lasttag.equals(ID_TAG)) {
                        userId = Integer.parseInt(p.getText());
                    }
                    
                    if (lasttag.equals(SENDER_TAG)) {
                        sender = p.getText();
                    }
                    //if (lasttag.equals(RECEIVER_TAG)) {
                     //   receivers = p.getText();
                    //}
                    if (lasttag.equals(TITLE_TAG)) {
                        title = p.getText();
                    }
                    if (lasttag.equals(TEXT_TAG)) {
                        text= p.getText();
                        
                    }
                    if (lasttag.equals(PRIORITY_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            priority=true;
                        }
                    }
                    if (lasttag.equals(IS_READ_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            isRead=true;
                        }
                    }
                    
                    break;
            }
        }
}

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getReceivers() {
        return receivers.toString();
    }

    public void setReceivers(String receivers) {
        this.receivers = new StringBuffer(receivers);
    }

    public boolean isPriority() {
        return priority;
    }

    public void setPriority(boolean priority) {
        this.priority = priority;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean isRead) {
        this.isRead = isRead;
    }

    

}